/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import database.databaseAdmin;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.User;

/**
 *
 * @author Rima
 */
public class MasukServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
     String userPath = request.getServletPath();
     HttpSession session = request.getSession();
     if (userPath.equals("/login")){
         if (session.getAttribute("loggedIn")== null){
              RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
                rd.forward(request, response);
         }else {
              RequestDispatcher rd = request.getRequestDispatcher("/paneladmin");
                rd.forward(request, response);
         }
         
     }else if(userPath.equals("/logout")){
         session.removeAttribute("loggedIn");
          RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
             rd.forward(request, response);
     }
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        
        
       
    
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         HttpSession session = request.getSession();

        String email = request.getParameter("email");
        String password = request.getParameter("password");

        try {
            databaseAdmin dbAdmin = new databaseAdmin();
            User dataUser = dbAdmin.cekLogin(email, password);
            if (dataUser != null) {
                //berhasil login
                  dataUser = dbAdmin.getUserEmail(email);
                  
                  dataUser.setStatusLogin(true);
                  session.setAttribute("sesi", dataUser);
                  session.setAttribute("email", email);
                  
//                session.setAttribute("email", dataUser.getEmail());
//                session.setAttribute("role", dataUser.getRole());
//                session.setAttribute("nama", dataUser.getNama());
//                session.setAttribute("ruang", dataUser.getId_ruang());
//                session.setAttribute("loggedIn", true);
                session.setMaxInactiveInterval(30 * 60);
                Cookie emailUser = new Cookie("email", email);
                Cookie namaUser = new Cookie ("nama", dataUser.getNama());
                emailUser.setMaxAge(30 * 60);
                namaUser.setMaxAge(30 * 60);
                response.addCookie(emailUser);
                response.addCookie(namaUser);
                
                if (dataUser.getRole() == 1) {
                    // menentukan role
                    response.sendRedirect("./panelsuperadmin");
                    //PrintWriter out = response.getWriter();
                    //out.print("berhasil");
                } else {
                    response.sendRedirect("./paneladmin");
                    PrintWriter out = response.getWriter();
                    out.print("berhasil");
                }
            } else {
                //gagal login
                RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
                rd.forward(request, response);
            }
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(MasukServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
